/*
 * XDemo.java, created on Nov 12, 2005
 * 
 * Copyright (c) 2005 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.xdemos.demo;

import java.awt.event.ActionEvent;
import java.util.Date;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JPanel;

import org.xfc.XApp;
import org.xfc.XAppFrame;
import org.xfc.components.XIconButton;
import org.xfc.dialog.XAboutBox;
import org.xfc.eventbus.XProgressEvent;
import org.xfc.eventbus.XStatusEvent;
import org.xfc.util.platform.XMacPlatform;
import org.xfc.util.platform.XPlatform;

import com.jgoodies.forms.layout.FormLayout;

/**
 *
 *
 * @author Devon Carew
 */
public class XDemo
	extends XApp
{
    
    public XDemo()
    {
        super("XDemo");
    }
    
	/**
	 * The main entry point into the application.
	 * 
	 * @param args
	 */
	public static void main(String[] args)
	{
		// Create the singleton instance of the application.
		final XDemo app = new XDemo();
		
		// Create the main application frame.
		XAppFrame frame = new XAppFrame(app);
		
		// Add the "Hello World" button.
		JPanel contentPanel = frame.getFrameContent();
		contentPanel.setLayout(new FormLayout("center:pref:grow", "pref:grow, pref, pref:grow"));
		contentPanel.add(new JButton(new HelloAction()), "1, 2");
		
		// Set up our I18N resources - these are read from XHelloWorld.properties.
		app.getResources().addI18NResources(XDemo.class);
		
		// Add an XML resource file for this application. This file can contain resources
		// like toolbars, menubars, menus, and actions. These are read from XHelloWorld.xml.
		app.getResources().addXMLResources(XDemo.class);
		
		// Add the menubar defined in the xml file.
		frame.setJMenuBar(app.getResources().getMenuBar("mainMenuBar"));
		
		// Add the toolbar defined in the xml file.
		frame.setToolBar(app.getResources().getToolBar("mainToolbar"));
		
		// Register our application-wide actions.
		app.registerGlobalAction("file-exit-action", app.getApplicationExitAction());
		
		app.registerGlobalAction("help-about-action", new AbstractAction() {
			public void actionPerformed(ActionEvent event) {
				XAboutBox aboutBox = new XAboutBox();
				
				aboutBox.setImage(XApp.getApp().getResources().getIcon("/eclipse/org.eclipse.ui/icons/full/wizban/new_wiz.gif"));
				aboutBox.setText("<html><b>XDemo v1.0</b><br><br>A demonstration of the XFC framework.</html>");
				
				aboutBox.setVisible(true);
			}
		});
        
		app.registerGlobalAction("tools-editor1-action", new AbstractAction() {
			public void actionPerformed(ActionEvent event) {
				app.showEditorsDemo();
			}
		});
		
        if (XPlatform.isMacintosh())
        {
            XMacPlatform platform = (XMacPlatform)XPlatform.getPlatform();
            
            platform.registerAboutAction(app.getResources().getAction("help-about-action"));
            platform.registerPreferencesAction(app.getResources().getAction("edit-preferences"));
            platform.registerQuitAction(app.getApplicationExitAction());
        }
        
		// Add a progress bar to the status line.
		app.getActiveAppFrame().getStatusBar().addProgressBar();
		
		// Add a custom component to the status bar.
		XIconButton b = new XIconButton(app.getResources().getAction("help-about-action"));
		
		app.getActiveAppFrame().getStatusBar().addComponent(b);
		
		// Show the window.
		frame.setTitle("XDemo");
		frame.setSize(450, 300);
		frame.show();
	}
	
	private void showEditorsDemo()
	{
		
		
		
	}
	
	private static class HelloAction
		extends AbstractAction
	{
		private boolean inProgress;
		
		/**
		 * Constructor for HelloAction.
		 */
		HelloAction()
		{
			super("Hello World!");
		}
		
		public void actionPerformed(ActionEvent arg0)
		{
			inProgress = !inProgress;
			
			XApp.getApp().getEventBus().publish(
				new XProgressEvent(this, inProgress ? XProgressEvent.INDETERMINATE_START : XProgressEvent.INDETERMINATE_END));
			
			XApp.getApp().getEventBus().publish(new XStatusEvent(this, "Hello World! " + new Date()));
		}
	}
	
}
